package com.cy.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.study.common.Response;
import com.cy.study.req.*;
import com.cy.study.res.ExamDetailRes;
import com.cy.study.res.ExamPageRes;
import com.cy.study.res.UserExamDetailRes;
import com.cy.study.res.UserExamPageRes;
import com.cy.study.service.ExamService;
import com.cy.study.service.QuestionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 试卷管理
 */
@RequiredArgsConstructor
@RequestMapping("/exam")
@RestController
@CrossOrigin
@Slf4j
public class ExamController {

    private final ExamService examService;
    private final QuestionService questionService;


    /**
     * 管理端试卷列表
     * @param req
     * @return
     */
    @PostMapping("/pageExam")
    public Response<Page<ExamPageRes>> pageExam(@RequestBody ExamPageReq req){
        Page<ExamPageRes> res = examService.pageExam(req);
        return Response.success(res);
    }

    /**
     * 管理端试卷详情
     * @param examId
     * @return
     */
    @GetMapping("/examDetail/{examId}")
    public Response<ExamDetailRes> examDetail(@PathVariable Integer examId){
        ExamDetailRes res = examService.examDetail(examId);
        return Response.success(res);
    }


    /**
     * 管理端添加试卷
     * @param req
     * @return
     */
    @PostMapping("/addExam")
    public Response<Integer> addExam(@RequestBody ExamAddReq req){
        Integer id = examService.addExam(req);
        return Response.success(id);
    }

    /**
     * 管理端删除试卷
     * @param req
     * @return
     */
    @PostMapping("/deleteExam")
    public Response deleteExam(@RequestBody ExamDeleteReq req){
        examService.deleteExam(req);
        return Response.success();
    }

    /**
     * 管理端添加题目
     * @param req
     * @return
     */
    @PostMapping("/addQuestion")
    public Response<Integer> addQuestion(@RequestBody QuestionAddReq req){
        Integer questionId = questionService.addQuestion(req);
        return Response.success(questionId);
    }

    /**
     * 管理端删除题目
     * @param req
     * @return
     */
    @PostMapping("/deleteQuestion")
    public Response deleteQuestion(@RequestBody QuestionDeleteReq req){
        questionService.deleteQuestion(req);
        return Response.success();
    }

    /**
     * 用户端试卷列表
     * @param req
     * @return
     */
    @PostMapping("/userExam")
    public Response<Page<UserExamPageRes>> userExamPage(@RequestBody UserExamPageReq req){
        Page<UserExamPageRes> res = examService.userExamPage(req);
        return Response.success(res);
    }

    /**
     * 用户端试卷详情
     * @param examId
     * @return
     */
    @GetMapping("/userDetail/{examId}")
    public Response<UserExamDetailRes> userExamDetail(@PathVariable Integer examId){
        UserExamDetailRes res = questionService.userExamDetail(examId);
        return Response.success(res);
    }

    /**
     * 用户做题
     * @param req
     * @return
     */
    @PostMapping("/userSubmitExam")
    public Response userSubmitExam(@RequestBody UserSubmitExamReq req){
        questionService.userSubmitExam(req);
        return Response.success();
    }



}
